/*
当庄模式玩家列表界面，显示当前包厢内有多少玩家，还有改变玩家积分功能
*/
var confige = require("confige");
cc.Class({
    extends: cc.Component,

    properties: {
        player_item:{
            default:null,
            type:cc.Node
        },
        parent:-1,
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.playerContent = this.node.getChildByName("playerView").getChildByName("view").getChildByName("content");
        this.playerItemBeginY = -50;
        this.playerItemOffsetY = -60;

        this.playerItemList = {};
        this.curPlayerData = {};
        this.curScoreData = {};

        this.scoreChangeLayer = this.node.getChildByName("scoreChangeLayer");
        this.changeLayerHead = this.scoreChangeLayer.getChildByName("head").getComponent("cc.Sprite");
        this.changeLayerNick = this.scoreChangeLayer.getChildByName("nickname").getComponent("cc.Label");
        this.changeLayerUid = this.scoreChangeLayer.getChildByName("uid").getComponent("cc.Label");
        this.changeLayerCurScore = this.scoreChangeLayer.getChildByName("curScore").getComponent("cc.Label");
        this.changeLayerChangeScore = this.scoreChangeLayer.getChildByName("changeScore").getComponent("cc.Label");
        this.curChangeScore = 0;
        this.curUserScore = 0;
        this.isInit = true;
    },

    updatePlayerList:function(){
        for(var i in this.playerItemList)
        {
            this.playerItemList[i].destroy();
        }
        this.playerItemList = {};

        var newContentHeight = 0;
        var playerItemCount = 0;
        var newPlayerItemY = this.playerItemBeginY;
        for(var i in this.curPlayerData)
        {
            if(parseInt(i) == parseInt(confige.userInfo.playerId))
                continue;
            playerItemCount++;

            var curPlayerData = this.curPlayerData[i];
            // console.log("curPlayerData======");
            // console.log(curPlayerData);

            var newHisPlayerCount = 0;
            for(var j in curPlayerData.player)
                newHisPlayerCount ++;
            var newPlayerItem = cc.instantiate(this.player_item);
            newPlayerItem.y = newPlayerItemY;
            this.playerItemList[i] = newPlayerItem;
            this.playerContent.addChild(newPlayerItem);
            newPlayerItem.active = true;
            newPlayerItemY += this.playerItemOffsetY;

            var self = this;
            var btnShow = newPlayerItem.getChildByName("mainBg").getComponent("cc.Button");
            var showCallBack = function(){
                console.log("showCallBack!!!!!!!!!!!");
                self.showChangeLayer(showCallBack.curData,showCallBack.uid,showCallBack.score);
            };
            showCallBack.curData = curPlayerData;
            showCallBack.uid = i;
            showCallBack.score = this.curScoreData[i];
            btnShow.node.on(cc.Node.EventType.TOUCH_END, showCallBack, self);

            var newHead = newPlayerItem.getChildByName("headNode").getChildByName("head").getComponent("cc.Sprite");
            window.DKTools.setFrameWithUrl(curPlayerData.head,newHead);
            var hisIndex = newPlayerItem.getChildByName("index").getComponent("cc.Label");
            hisIndex.string = playerItemCount;
            var id = newPlayerItem.getChildByName("id").getComponent("cc.Label");
            id.string = i;
            newPlayerItem.score = newPlayerItem.getChildByName("score").getComponent("cc.Label");
            newPlayerItem.score.string = this.curScoreData[i];
            var nick = newPlayerItem.getChildByName("nick").getComponent("cc.Label");
            nick.string = curPlayerData.nickname;

        }
        if(newContentHeight < 500)
            newContentHeight = 500;
        this.playerContent.height = -newPlayerItemY + 100;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
        this.updatePlayerList();
    },

    hideLayer:function(){
        this.node.active = false;
    },

    showChangeLayer:function(userData,uid,score){
        console.log(userData);
        window.DKTools.setFrameWithUrl(userData.head,this.changeLayerHead);
        this.changeLayerNick.string = userData.nickname;
        this.changeLayerUid.string = uid;
        this.changeLayerCurScore.string = "当前粮草:" + score;
        this.curUserScore = score;
        this.resetChangeLayer();
        this.scoreChangeLayer.active = true;
    },

    hideScoreChangeLayer:function(){
        this.scoreChangeLayer.active = false;
    },

    btnScoreChangeLayerClick:function(event,customEventData){
        var index = parseInt(customEventData);
        switch(index){
            case 0:
            this.curChangeScore += 10;
            break;
            case 1:
            this.curChangeScore += 100;
            break;
            case 2:
            this.curChangeScore -= 10;
            break;
            case 3:
            this.curChangeScore -= 100;
            break;
            case 4:
            this.curChangeScore += 1000;
            break;
            case 5:
            this.curChangeScore = -this.curUserScore;
            break;
            case 100:
            var self = this;
            pomelo.request("connector.entryHandler.sendData", {"code":"houseOwnerCmd","params":{code:"changeScore",uid:parseInt(this.changeLayerUid.string),score:this.curChangeScore}}, function(data) {
                console.log("changeScore @@@@@@@@");
                console.log(data.flag);
                if(data.flag.uid){
                    self.playerItemList[data.flag.uid].score.string = data.flag.score;
                    self.curScoreData[data.flag.uid] = data.flag.score;
                    // self.scoreChangeLayer.active = false;
                    // self.node.active = false;
                    pomelo.request("connector.entryHandler.sendData", {"code":"houseOwnerCmd","params":{code:"getUserMap"}}, function(data) {
                        console.log("获取玩家列表");
                        console.log(data);
                        for(var i in data.flag.userMap)
                            self.parent.userHistoryList[parseInt(i)] = data.flag.userMap[i];
                            
                        self.curScoreData = data.flag.userScores;    
                        self.curPlayerData = data.flag.userMap;
                        self.showLayer();
                        self.scoreChangeLayer.active = false;
                    });
                }
            });
            break;
            default:
            break;
        };
        this.changeLayerChangeScore.string = this.curChangeScore;
    },

    resetChangeLayer:function(){
        this.curChangeScore = 0;
        this.changeLayerChangeScore.string = this.curChangeScore;
    },
});
